<template>
  <el-card>
    <el-table border style="margin: 10px 0px" :data="SkuAttr">
      <el-table-column
        label="序号"
        type="index"
        align="center"
        width="80px"
      ></el-table-column>
      <el-table-column
        label="名称"
        show-overflow-tooltip
        width="250px"
        prop="skuName"
      ></el-table-column>
      <el-table-column
        label="描述"
        show-overflow-tooltip
        width="250px"
        prop="skuDesc"
      ></el-table-column>
      <el-table-column label="图片" width="250px">
        <template #="{ row, index }">
          <img
            :src="row.skuDefaultImg"
            alt="图片损坏"
            style="width: 100px; height: 100px"
          />
        </template>
      </el-table-column>
      <el-table-column
        label="重量"
        width="250px"
        prop="weight"
      ></el-table-column>
      <el-table-column
        label="价格"
        width="250px"
        prop="price"
      ></el-table-column>
      <el-table-column label="操作" width="350px" fixed="right">
        <template #="{ row, index }">
          <el-button
            v-if="row.isSale == 0"
            type="primary"
            icon="Top"
            circle
            @click="updateSale(row)"
          ></el-button>
          <el-button
            v-else
            type="info"
            icon="Bottom"
            circle
            @click="updateSale(row)"
          ></el-button>
          <!-- <el-button type="primary" :icon="row.isSale == 1?'Top':'Bottom'" circle /> -->
          <el-button type="primary" icon="Edit" circle @click="updateSku" />
          <el-button
            type="info"
            icon="InfoFilled"
            circle
            @click="findSku(row)"
          />
          <el-button
            type="danger"
            icon="Delete"
            circle
            @click="deleteSku(row)"
          />
        </template>
      </el-table-column>
    </el-table>
    <!-- 分页器 -->
    <el-pagination
      v-model:current-page="pageNo"
      v-model:page-size="pageSize"
      :page-sizes="[1, 2, 3, 4]"
      :background="true"
      layout="prev, pager, next, jumper, -> ,total, sizes"
      :total="total"
      @current-change="getHasSku"
      @size-change="changeSize"
    />

    <!-- 抽屉组件:展示商品详情 -->
    <el-drawer v-model="drawer">
      <!-- 标题部分 -->
      <template #header>
        <h4>SKU商品详情</h4>
      </template>
      <template #default>
        <el-row style="margin: 10px 0px">
          <el-col :span="6">名称</el-col>
          <el-col :span="18">{{ skuInfo.skuName }}</el-col>
        </el-row>
        <el-row style="margin: 10px 0px">
          <el-col :span="6">描述</el-col>
          <el-col :span="18">{{ skuInfo.skuDesc }}</el-col>
        </el-row>
        <el-row style="margin: 10px 0px">
          <el-col :span="6">价格</el-col>
          <el-col :span="18">{{ skuInfo.price }}</el-col>
        </el-row>
        <el-row style="margin: 10px 0px">
          <el-col :span="6">平台属性</el-col>
          <el-col :span="18">
            <el-tag
              style="margin: 5px"
              v-for="item in skuInfo.skuAttrValueList"
              :key="item.id"
            >
              {{ item.valueName }}
            </el-tag>
          </el-col>
        </el-row>
        <el-row style="margin: 10px 0px">
          <el-col :span="6">销售属性</el-col>
          <el-col :span="18">
            <el-tag
              style="margin: 5px"
              v-for="item in skuInfo.skuSaleAttrValueList"
              :key="item.id"
            >
              {{ item.saleAttrValueName }}
            </el-tag>
          </el-col>
        </el-row>
        <el-row style="margin: 10px 0px">
          <el-col :span="6">商品图片</el-col>
          <el-col :span="18">
            <el-carousel :interval="4000" type="card" height="200px">
              <el-carousel-item
                v-for="item in skuInfo.skuImageList"
                :key="item.id"
              >
                <img
                  :src="item.imgUrl"
                  alt=""
                  style="width: 100%; height: 100%"
                />
              </el-carousel-item>
            </el-carousel>
          </el-col>
        </el-row>
      </template>
    </el-drawer>
  </el-card>
</template>

<script setup lang="ts">
import { ref, onMounted } from 'vue'
import {
  reqSkuList,
  reqOnSale,
  reqCannelSale,
  reqGetSkuInfo,
  reqDeleteSku,
} from '../../../api/product/sku'
import type { SkuData, ResponseSkuData } from '../../../api/product/sku/type.ts'
import { ElMessage, ElMessageBox } from 'element-plus'

// 分页器默认显示页码
let pageNo = ref<number>(1)
// 分页器某人每页显示条数
let pageSize = ref<number>(1)
// 总条数
let total = ref<number>(0)
// 表格数据展示SKU对象
let SkuAttr = ref<SkuData[]>([])

// 控制抽屉显示与隐藏的字段
let drawer = ref<boolean>(false)

// 保存抽屉SkuInfo信息
let skuInfo = ref<any>({})

// 获取分页数据
const getHasSku = async (pager = 1) => {
  pageNo.value = pager
  let result: ResponseSkuData = await reqSkuList(pageNo.value, pageSize.value)

  // 判断请求是否成功
  if (result.code == 200) {
    total.value = result.data.total
    SkuAttr.value = result.data.records
  }
}

//分页器下拉菜单发生变化的时候触发
const changeSize = (obj: any) => {
  getHasSku()
}

// 更新SKU商品上下架状态
const updateSale = async (row: SkuData) => {
  // 如果当前的商品isSale==1，说明当前商品是上架的状态 --> 更新为下架
  // 否则else情况与上面情况相反
  if (row.isSale == 1) {
    // 下架操作
    await reqCannelSale(row.id)
    // 提示信息
    ElMessage({ type: 'success', message: '下架成功' })
    // 发送请求获取当前更新完毕的全部已有的SKU
    getHasSku(pageNo.value)
  } else {
    // 下架操作
    await reqOnSale(row.id)
    // 提示信息
    ElMessage({ type: 'success', message: '上架成功' })
    // 发请求获取当前更新完毕的全部已有的SKU
    getHasSku(pageNo.value)
  }
}

//更新已有的SKU
const updateSku = () => {
  ElMessage({ type: 'success', message: '程序员在努力的更新中....' })
}

// 查看商品详情按钮的回调
const findSku = async (row: SkuData) => {
  // 展示抽屉
  drawer.value = true

  // 获取Sku接口信息
  let result = await reqGetSkuInfo(row.id)
  skuInfo.value = result.data
}

// 删除SKU详情
const deleteSku = (row: SkuData) => {
  ElMessageBox.confirm('是否确认删除该SKU商品?', '提示', {
    confirmButtonText: '确认',
    cancelButtonText: '取消',
    type: 'warning',
  })
    .then(async () => {
      let result = await reqDeleteSku(row.id)
      if (result.code == 200) {
        ElMessage({
          type: 'success',
          message: '删除SKU商品成功',
        })
        // 重新获取当前页的SKU列表数据
        getHasSku(pageNo.value)
      } else {
        ElMessage({
          type: 'error',
          message: '删除SKU商品失败',
        })
      }
    })
    .catch(() => {
      ElMessage({
        type: 'info',
        message: '已取消删除操作',
      })
    })
}

onMounted(() => {
  getHasSku()
})
</script>

<style lang="scss" scoped></style>
